<?php

class authorities{
	protected $records;

	function __construct(){
		include_once("../../../api/authorities.php");
		$this->records = $config;
	}

	function getAuthorityList(){
		return( @array_values($this->records) );
	}

	function getCount(){
		return (count($this->records));
	}

	function getAuthById($id){
		return (isset($this->records[$id])) ? $this->records[$id] : false;
	}

	function addAuthority($authConfig, $s){
		$id = uniqid();

 		$connection = mysql_connect($s["server"], $s["userName"], $s["pass"]);
		$dbSelect = mysql_select_db($s["name"],$connection);
		$query = sprintf("SELECT COUNT(*) as sz FROM splist_%s"
			, mysql_escape_string($authConfig)
		);
		$result = mysql_query($query);
		$tmp = @mysql_fetch_assoc($result);
		$species = $tmp["sz"];

		$query = sprintf("SELECT COUNT(*) as sz FROM genlist_%s"
			, mysql_escape_string($authConfig)
		);
		$result = mysql_query($query);
		$tmp = @mysql_fetch_assoc($result);
		$genus = $tmp["sz"];

		$createdDate = Date('d-m-Y');
		$this->records[$id]["id"] = $id;
		$this->records[$id]["active"] = 'true';
		$this->records[$id]["name"] = $authConfig;
		$this->records[$id]["created"] = $createdDate;
		$this->records[$id]["lastUpdated"] = $createdDate;
		$this->records[$id]["database"] = $s['id'];
		$this->records[$id]["species"] = $species;
		$this->records[$id]["genus"] = $genus;

		$results = $this->createConfig();
		$results = "<?php\r\n" . $results . "?>";
		$res = file_put_contents('../../../api/authorities.php', $results);
		return($res);
	}

	function deleteAuthority($id, $dbId) {
		$res=0;
		foreach($this->records as $key => $value){
			if($value['name']==$id){
				unset($this->records[$key]);
				$results = $this->createConfig();
				$results = "<?php\r\n" . $results . "?>";
				$res = file_put_contents('../../../api/authorities.php', $results);
			}
		}
		if($res){
			include_once("../../../api/database.php");
			foreach($config as $key => $value){				
				if($value['id'] == $dbId){
					$s = $config[$key];
					break;
				}
			}

			$connection = mysql_connect($s["server"], $s["userName"], $s["pass"]);
			$dbSelect = mysql_select_db($s["name"],$connection);

			$query = sprintf("DROP TABLE auth_abbrev_%s, genlist_%s, genus_id_matches_%s, species_id_matches_%s, splist_%s"
				, mysql_escape_string($id)
				, mysql_escape_string($id)
				, mysql_escape_string($id)
				, mysql_escape_string($id)
				, mysql_escape_string($id)
			);

			$result = mysql_query($query);
			return ($res);
		} else{
			return(false);
		}
	}

	private function createConfig() {
		$output = '';
		if(is_array($this->records) && count($this->records)) {
		foreach($this->records as $name => $value) {
			if(is_array($value)) {
			foreach($value as $name2 => $value2) {
				if(is_array($value2)) {
				foreach($value2 as $name3 => $value3) {
					if(is_array($value3)) {
					foreach($value3 as $name4 => $value4) {
						if(is_array($value4)) {
						foreach($value4 as $name5 => $value5) {
							$output .= sprintf("\$config['%s']['%s']['%s']['%s']['%s'] = '%s';\r\n", $name, $name2, $name3, $name4, $name5,  stripslashes($value5) );
						}
						} else {
							$output .= sprintf("\$config['%s']['%s']['%s']['%s'] = '%s';\r\n", $name, $name2, $name3, $name4, stripslashes($value4) );
						}
					}
					} else {
						$output .= sprintf("\$config['%s']['%s']['%s'] = '%s';\r\n", $name, $name2, $name3, stripslashes($value3));
					}
				}
				} else {
					$output .= sprintf("\$config['%s']['%s'] = '%s';\r\n", $name, $name2, stripslashes($value2) );
				}
			}
			} else {
				$output .= sprintf("\$config['%s'] = '%s';\r\n", $name, stripslashes($value) );
			}
		}
		return($output);
		}
		return false;
	}

	function getValue($source, $id, $data){
		include("../../../api/database.php");
		include("../../../api/authorities.php");

		$s = $config[$id];
		$connection = mysql_connect($s["server"], $s["userName"], $s["pass"]);
		$dbSelect = mysql_select_db($s["name"], $connection);

		if ($data["sort"] == "genus") $data["sort"] = "GENUS_ORIG";
		$sort = ($data["sort"]) ? $data["sort"] . " " . $data["dir"] : " GENUS_ORIG, SPECIES ";
		
		$query = sprintf("SELECT * FROM `splist_%s` ORDER BY %s LIMIT %s,%s;"
			, mysql_escape_string($config[$source]["name"])
			,	$sort
			, ($data["start"]) ? $data["start"] : 0
			,	($data["limit"]) ? $data["limit"] : 50
		);

		$result = mysql_query($query);

		if($result){
			while($row = mysql_fetch_array($result)){

				$family = '';
				$genus = $row['GENUS_ORIG'];
				$species = $row['SPECIES'];

				if($species != '') {
					$posts[] = array(
							'family' => $family
						, 'genus' => $genus
						, 'species' => $species
					);
				}
			}
			return (array('data' => $posts, 'totalCount' => $config[$source]["species"]));
		} else {
			return (false);
		}

 	}
}
?>